package com.itheima.reggie.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/category/findAll")
    public ResultInfo findAll() {
        List<Category> categoryList = categoryService.findAll();
        return ResultInfo.success(categoryList);
    }

    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category) {
        categoryService.save(category);

        return ResultInfo.success(null);
    }

    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category) {
        categoryService.update(category);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/category")
    public ResultInfo deleteCategory(Long id) {
        ResultInfo resultInfo = categoryService.delete(id);
        return resultInfo;
    }
    //根据类型查询分类列表
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){
        List<Category> categoryList =  categoryService.findByType(type);
        return ResultInfo.success(categoryList);
    }

}
